package com.spring.mvc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期时间操作工具类
 * @author liujian
 *
 */
public class DateUtil {

	/**
	 * 以yyyy-MM-dd格式获取当前日期
	 * @return
	 */
	public static String getCurrentDate() {
		SimpleDateFormat formater;
		try {
			formater = new SimpleDateFormat("yyyy-MM-dd");
			return formater.format(new Date());
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 根据指定格式获取当前时间
	 * @param sFmt
	 * @return
	 */
	public static String getCurrentFormatDate(String sFmt) {
		SimpleDateFormat formater;
		try {
			formater = new SimpleDateFormat(sFmt);
			return formater.format(new Date());
		} catch (Exception e) {
			return "";
		}
	}

	public static String getCurrentTime1(int timeType, int num) {
		SimpleDateFormat formater;
		try {
			formater = new SimpleDateFormat("HH:mm:ss");
			Calendar cal = Calendar.getInstance();
			cal.add(timeType, num);

			return formater.format(cal.getTime());
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 获取当前num天前后固定格式的时间
	 * @param num
	 * @return
	 */
	public static String getCurrentDate(int num) {
		SimpleDateFormat formater;
		try {
			formater = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, num);
			return formater.format(cal.getTime());
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 根据给定标识获取前后固定格式的时间
	 * @param dateField 表示 天，月，星期等
	 * @param num
	 * @return
	 */
	public static String getDateTime(int dateField, int num) {
		SimpleDateFormat formater;
		try {
			formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar cal = Calendar.getInstance();
			cal.add(dateField, num);

			return formater.format(cal.getTime());
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getCurrentTime() {
		SimpleDateFormat formater;
		try {
			formater = new SimpleDateFormat("HH:mm:ss");
			return formater.format(new Date());
		} catch (Exception e) {
			return "";
		}
	}

	public static String getCurrentDateTime() {
		SimpleDateFormat formater;
		try {
			formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return formater.format(new Date());
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 根据指定格式获取当前时间
	 * 
	 * @param format
	 * @return
	 */
	public static String getCurrentDate(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String date = sdf.format(new Date());
		return date;
	}

	public static String ConvertToTime(String s) {
		String strReturn = "";
		try {
			if (s.length() == 5) {
				strReturn = "0" + s.substring(0, 1) + ":" + s.substring(1, 3)
						+ ":" + s.substring(3);
			} else
				strReturn = s.substring(0, 2) + ":" + s.substring(2, 4) + ":"
						+ s.substring(4);
		} catch (Exception e) {
			strReturn = "";
		}
		return strReturn;
	}

	public static String ConvertToTime1(String s) {
		String strReturn = "";
		try {
			if (s.length() == 5) {
				strReturn = "0" + s.substring(0, 1) + "时" + s.substring(1, 3)
						+ "分" + s.substring(3) + "秒";
			} else
				strReturn = s.substring(0, 2) + "时" + s.substring(2, 4) + "分"
						+ s.substring(4, 6) + "秒";
		} catch (Exception e) {
			strReturn = "";
		}
		return strReturn;
	}

	/**
	 * 根据指定格式获取当前时间前后day天的日期
	 * 
	 * @param day
	 * @return
	 */
	public static String getBeforeOrAfterDay(String format, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, day);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	/**
	 * 比较日期前后关系
	 * 
	 * @param startDate
	 *            前者
	 * @param endDate
	 *            后者
	 * @return 正 前者大，0相等 ，负后者大
	 */
	public static long compareDate(String format, String startDate,
			String endDate) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		long start = 0;
		long end = 0;
		try {
			start = sdf.parse(startDate).getTime();
			end = sdf.parse(endDate).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return start - end;
	}

	public static String ConvertToDate(String s) {
		String strReturn = "";
		try {
			if (s.length() == 6)
				s = "20" + s;

			if (s.length() == 8)
				strReturn = s.substring(0, 4) + "-" + s.substring(4, 6) + "-"
						+ s.substring(6);
		} catch (Exception e) {
			strReturn = "";
		}
		return strReturn;
	}

	public static String ConvertToDate1(String s) {
		String strReturn = "";
		try {
			if (s.length() == 6)
				s = "20" + s;

			if (s.length() == 8)
				strReturn = s.substring(0, 4) + "年" + s.substring(4, 6) + "月"
						+ s.substring(6) + "日";
		} catch (Exception e) {
			strReturn = "";
		}
		return strReturn;
	}

	public static String getCurrentDate1() {
		SimpleDateFormat formater;
		try {
			formater = new SimpleDateFormat("yyyy年MM月dd日");
			return formater.format(new Date());
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 由给定固定格式字符串转换成日期
	 * @param sDate
	 * @return
	 */
	public static Date parserDate(String sDate) {
		Date d = null;
		try {
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
			d = fmt.parse(sDate);
		} catch (Exception e) {
			d = null;
		}
		return d;
	}

	/**
	 * 将给定固定格式字符串转换成时间
	 * @param sDate
	 * @return
	 */
	public static Date parserDateTime(String sDate) {
		Date d = null;
		try {
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			d = fmt.parse(sDate);
		} catch (Exception e) {
			e.printStackTrace();
			d = null;
		}
		return d;
	}

	/**
	 * 将long转换成固定格式时间
	 * @param lTime
	 * @return
	 */
	public static String getDateTime(long lTime) {
		SimpleDateFormat formater;
		try {
			formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return formater.format(new Date(lTime));
		} catch (Exception e) {
			return "";
		}
	}

	public static String getDateTime(long lTime, String fmt) {
		SimpleDateFormat formater;
		try {
			formater = new SimpleDateFormat(fmt);
			return formater.format(new Date(lTime));
		} catch (Exception e) {
			return "";
		}
	}
	
	public static String getDateTime(Date date){
		SimpleDateFormat formater;
		try {
			formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return formater.format(date);
		} catch (Exception e) {
			return "";
		}
	}
}
